home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / OvalsDoodadd-1.2-c / Origin < prev    next >
Encoding:
Text File  |  1994-12-04  |  9.6 KB  |  360 lines  |  [TEXT/EDIT]

  1. NOTE:  This old source has been updated as "NewDisplay" and
  2. runs in CW and TC.
  3. --------------------------------------------------------------------
  4. /* This sample program allows a user to open a file and print it's contents
  5. ** to a window on the Mac screen.  To give the compiled program an Icon
  6. ** copy the resources from Display.rsrc (use Rmover or Resource Editor) and
  7. ** paste them into the compiled file.  Then use SetFile to change the creator
  8. ** name of the compiled file to DFIL and set the bundle bit.  When you 
  9. ** return to the finder the program icon should have an icon.
  10. ** By Greg Corson
  11. ** 19141 Summers Drive
  12. ** South Bend, IN 46637
  13. ** (219) 272-2136
  14. ** UUCP: {ihnp4 | ucbvax}!pur-ee!kangaro!milo
  15. ** ARPA: pur-ee!kangaro!milo@Purdue.ARPA
  16. ** EDU: kangaro!milo@ee.Purdue.EDU
  17. ** Or call my BBS at (219) 277-5825
  18. */
  19.  
  20. #include <qd.h>  
  21. #include <win.h>
  22. #include <menu.h>
  23. #include <event.h>
  24. #include <pack.h>
  25. #include <font.h>
  26.  
  27. #define LASTMENU 4
  28. #define APPLEMENU 1
  29. #define FILEMENU 256
  30. #define EDITMENU 257
  31. #define STOPMENU 258
  32. #define NULL 0L
  33. #define FALSE 0
  34. #define TRUE 1
  35. #define eoferr (-39)
  36.  
  37. rgnhandle    updateregn;
  38. menuhandle   mymenus[LASTMENU+1];
  39. rect         screenrect, dragrect, prect;
  40. boolean      doneflag, temp;
  41. eventrecord  myevent;
  42. int          code, refnum;
  43. windowrecord wrecord;
  44. windowptr    mywindow, whichwindow;
  45. grafptr      temport;
  46. int          themenu, theitem;
  47. int          fileopen,wide,fd1;
  48. long         count;
  49. char         tempbuf[32];
  50.  
  51. main()
  52. {
  53.    #include <qdvars.h>    /* quickdraw globals */
  54.    int i,j;
  55.  
  56. /* Initialize variables */
  57.  
  58.    j = 0;
  59.    doneflag = FALSE;
  60.    fileopen = FALSE;
  61.    
  62. /* Initialize quickdraw, fonts, events, windows, menus, dialogs and cursor */
  63.    
  64.    initgraf(&theport);
  65.    initfonts();
  66.    flushevents(everyevent, 0);
  67.    initwindows();
  68.    initmenus();
  69.    teinit();
  70.    initdialogs(NULL);
  71.    initcursor();
  72.  
  73. /* Create an empty region pointer for use by scrollrect later */
  74.  
  75.    updateregn=newrgn();
  76.  
  77. /* Setup the menu bar */
  78.  
  79.    setupmenus();
  80.    
  81. /* Setup the drag rectangle so part of the window will always be visible */
  82.  
  83.    setrect(&screenrect, 4, 40, 508, 338);
  84.    setrect(&dragrect, 4, 24, screenrect.a.right-4, screenrect.a.bottom-4);
  85.    
  86. /* Create the window and set the current port to the window port */
  87.    
  88.    mywindow = newwindow(&wrecord, &screenrect, "Display a file", TRUE, 0,
  89.              (long)-1, FALSE, (long)0);
  90.    setport(mywindow);
  91.  
  92. /* get the rectangle for the current window and put it in prect */
  93.  
  94.    blockmove(&theport->portrect, &prect, (long)sizeof prect);
  95.    wide = prect.a.right  - prect.a.left;
  96.    
  97. /* Now that the window and menus are drawn set the window font to monaco 9 */
  98.    
  99.    textfont(monaco);
  100.    textsize(9);
  101.    moveto(prect.a.left+1,prect.a.bottom-2);
  102.  
  103. /* Main loop to process events */
  104.    
  105.    do {
  106.  
  107. /**** If a file is open copy a line to the output window */
  108.  
  109.       if(fileopen)
  110.          {
  111.      count=32;
  112.      fsread(fd1, &count, tempbuf);
  113.      if(count == 0)
  114.         {
  115.         fsclose(fd1);
  116.         fileopen=FALSE;
  117.         moveto(prect.a.left+1,prect.a.bottom-2);
  118.         scrollrect(&prect,0,-11,updateregn);
  119.         drawstring("-------End of File-------");
  120.         scrollrect(&prect,0,-11,updateregn);
  121.         moveto(prect.a.left+1,prect.a.bottom-2);
  122.         }
  123.      else
  124.         {
  125.         for(i = 0; i < count; i++)
  126.            {
  127.            if(tempbuf[i] > 31)
  128.               drawchar(tempbuf[i]);
  129.            else
  130.               {
  131. /**************** Scroll window if we get a carriage return */
  132.                   if(tempbuf[i] == '\r')
  133.              {
  134.              j = 0;
  135.              scrollrect(&prect,0,-11,updateregn);
  136.              moveto(prect.a.left+1,prect.a.bottom-2);
  137.              }
  138. /**************** Expand tabs by outputting spaces */
  139.                   if(tempbuf[i] == '\011')
  140.              {
  141.              drawchar(' ');
  142.              j++;
  143.              for(;j & 07;j++)
  144.                 drawchar(' ');
  145.              }
  146.           }
  147.            }
  148.         }
  149.      }
  150.  
  151. /**** Get the next event */
  152.  
  153.       systemtask();
  154.       temp = getnextevent(everyevent, &myevent);
  155.       switch (myevent.what)
  156.          {
  157.      case mousedown:  /* mouse down, call findwindow to figure out where */
  158.         code = findwindow(&myevent.where, &whichwindow);
  159.         switch (code)
  160.            {
  161.            case inmenubar:    /* in meun bar, execute the menu command */ 
  162.               docommand(menuselect(&myevent.where));
  163.           break;
  164.                case insyswindow:    /* in desk accessory, call desk manager */
  165.               systemclick(&myevent, whichwindow); 
  166.           break;
  167.            case indrag:    /* in drag, call dragwindow to move it */
  168.               dragwindow(whichwindow, &myevent.where, &dragrect);
  169.               break;
  170.            case incontent:    /* in content area, make application window the frontmost */
  171.            if (whichwindow != frontwindow())
  172.               selectwindow(whichwindow);
  173.            break;
  174.            }
  175.         break;
  176.      case keydown:    /* If keydown event, check for menu command key */
  177.         if(myevent.modifiers & cmdkey)
  178.            docommand(menukey((char)(myevent.message & 0377)));
  179.         break;
  180.      case autokey:
  181.         break;
  182.      case activateevt:    /* Application window becomming active, do nothing */
  183.         if((myevent.modifiers & 1)&&(((windowptr)myevent.message) == mywindow))
  184.            {
  185.            disableitem(mymenus[3],0);
  186.            enableitem(mymenus[2],0);
  187.            drawmenubar();
  188.            }
  189.         else
  190.            {
  191.            enableitem(mymenus[3],0);
  192.            disableitem(mymenus[2],0);
  193.            drawmenubar();
  194.            }
  195.         break;
  196.      case updateevt:    /* Update event, update the window frame */
  197.         if(((windowptr)myevent.message) == mywindow)
  198.            {
  199.            beginupdate(mywindow);
  200.            endupdate(mywindow);
  201.            }
  202.         break;
  203.     }
  204.     } while (doneflag == 0);
  205. }
  206.  
  207. /*---------------------------------------------------------------------------*/
  208. /* setupmenus()---This subroutine sets up the menu bar and reads in the desk
  209. ** accessory menu
  210. */
  211.  
  212. setupmenus()
  213. {
  214.    int i;
  215.  
  216. /* Apple menu, \024 is the apple character, adresmenu call loads all type DRVR resources */
  217.    mymenus[1] = newmenu(APPLEMENU, "\024");
  218.    appendmenu(mymenus[1], "About \"Display a File\";(-");
  219.    addresmenu(mymenus[1], "DRVR");
  220. /* File menu with open, close and quit selections */
  221.    mymenus[2] = newmenu(FILEMENU, "File");
  222.    appendmenu(mymenus[2], "Open/O;Close/C;Quit/Q");
  223. /* Edit menu with cut, copy and paste */
  224.    mymenus[3] = newmenu(EDITMENU, "Edit");
  225.    appendmenu(mymenus[3], "Undo;(-;Cut;Copy;Paste;Clear;(-;Show Clipboard");
  226. /* Stop scroll menu */
  227.    mymenus[4] = newmenu(STOPMENU,"Click Here to Pause Printout");
  228.    appendmenu(mymenus[4], "Release mouse button to resume printout");
  229.    for (i=1; i<=LASTMENU; i++)
  230.       insertmenu(mymenus[i], 0);
  231. /* Draw the completed menu bar */
  232.    drawmenubar();
  233. }
  234. /*---------------------------------------------------------------------------*/
  235. /* docommand(themenu, theitem)---this subroutine processes commands from the
  236. ** menu bar.  Themenu is the menu ID, theitem is the item number in the menu
  237. */
  238.  
  239. docommand(themenu, theitem)
  240. int themenu, theitem;
  241. {
  242.    char name[256];
  243.    point  openp;
  244.    sfreply rep;
  245.    sftypelist typelist;
  246.    int i;
  247.  
  248. /* Switch to decide what menu the cursor is in */
  249.    
  250.    switch (themenu)
  251.       {
  252.       case APPLEMENU:    /* Mouse down in apple menu */
  253. /******* Item one is the "about Display a file" box */
  254.          if(theitem == 1)
  255.         {
  256.         textfont(systemfont);
  257.         textsize(12);
  258.         eraserect(&prect);
  259.         moveto(prect.a.left,prect.a.top+70);
  260.         center("Display a file program");
  261.         center("Copyright 1985 by Greg Corson");
  262.         center("Kangaroo Koncepts, Inc.");
  263.         center("19141 Summers Drive");
  264.         center("South Bend, IN 46637");
  265.         center("(219) 277-5306");
  266.         textfont(monaco);
  267.             textsize(9); 
  268.         move(0,-3);
  269.         center("Feel free to give this program away to all your friends.");
  270.         center("It should NOT be sold for profit.  Be sure to try our");
  271.         center("Computer Based Communications System \"The Connection\"");
  272.         center("Free demo line (219) 277-5825 available 24 hours at 300 or");
  273.         center("1200 baud.  Be sure to look at the \"MacTech\" special");
  274.         center("interest group for information of interest to Mac");
  275.         center("programmers and the \"macintosh\" SIG for general info.");
  276.         pretty();
  277.         moveto(prect.a.left+1,prect.a.bottom-2);
  278.         }
  279. /******* The rest of the items are desk accessorys */
  280.          else
  281.         {
  282.         getitem(mymenus[1], theitem, name);
  283.             refnum = opendeskacc(name);
  284.         setport(mywindow);
  285.         }
  286.      break;
  287.       case FILEMENU:    /* Mouse down in file menu */
  288.          switch(theitem)
  289.         {
  290.         case 1:        /* Open file */
  291.            openp.a.v = 100;
  292.            openp.a.h = 60;
  293.            strncpy(&typelist[0],"TEXT",4);
  294.            sfgetfile(&openp.a,"",NULL,1, typelist, NULL, &rep);
  295.            if(rep.good)
  296.               {
  297.           if(fileopen)
  298.                  {
  299.              fsclose(fd1);
  300.              fileopen = FALSE;
  301.              }
  302.           if(fsopen(rep.fname,rep.vrefnum,&fd1) == noerr)
  303.              {
  304.              scrollrect(&prect,0,-11,updateregn);
  305.              fileopen=TRUE;
  306.              }
  307.           }
  308.            break;
  309.         case 2:        /* Close file */
  310.            if(fileopen)
  311.               {
  312.           fsclose(fd1);
  313.           fileopen = FALSE;
  314.           }
  315.            break;
  316.         case 3:        /* Quit */
  317.            if(fileopen)
  318.               {
  319.           fsclose(fd1);
  320.           fileopen = FALSE;
  321.           }
  322.            doneflag = 1;
  323.            break;
  324.         }
  325.      break;
  326.       case EDITMENU:    /* Process system edit events */
  327.          systemedit(theitem-1);
  328.      break;
  329.    }
  330.    hilitemenu(0);
  331. }
  332. /* Center a string in the window */
  333. center(str)
  334. char   *str;
  335. {
  336.    move(((wide-stringwidth(str))/2), 0);
  337.    drawstring(str);
  338.    move(-(mywindow->pnloc.a.h),(mywindow->txsize)+2);
  339. }
  340. /* draw a pretty design */
  341. pretty()
  342. {
  343.    int j;
  344.    rect tmprec;
  345.    
  346.    blockmove(&prect, &tmprec, (long)sizeof prect);
  347.    for(j=0;j < 12;j++)
  348.       {
  349.       frameoval(&tmprec);
  350.       insetrect(&tmprec,6,0);
  351.       }
  352.    blockmove(&prect, &tmprec, (long)sizeof prect);
  353.    for(j=0;j < 9;j++)
  354.       {
  355.       frameoval(&tmprec);
  356.       insetrect(&tmprec,0,6);
  357.       }
  358. }
  359.    
  360.